package com.example.forum.exception;

import com.example.forum.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 自定义异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(CustomException.class)
    public Result<String> exceptionHandler(CustomException e) {
        log.error("自定义异常处理: {}", e.getMessage());
        return Result.error(e.getMessage());
    }

    /**
     * 全局异常处理
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception e) {
        if(e.getMessage().contains("favicon.ico")){
            return Result.success();
        }
        log.error("全局异常处理: {}", e.getMessage());
        return Result.error(e.getMessage());
    }
}
